import json
import sys, os, re
from bs4 import BeautifulSoup
from lib.log import DBG, INFO, ERROR, EXCEPTION
from lib.m3u8 import M3U8

class ParseTest(object):

    def parse_class_page(self, from_url, html_text, params):
        # html_text = open("第1页.html")
        pattern = re.compile(r'<option value=([^>]*)>([^<]*)</option>')
        matches = pattern.findall(html_text)
        page_count = matches[-1][1]
        # 提取匹配结果中的值
        # values = [match[1] for match in matches]
        print(page_count)  # 输出: ['33', '34']

        return page_count

    def _get_res_id_from_href(self, href):
        match = re.search(r'video_detail/(\d+)', href)
        res_id = None
        if match:
            res_id = match.group(1)
        else:
            ERROR(f"未解析出资源ID: {href}")

        return res_id


if __name__ == '__main__':
    # save_dir = f"{os.path.abspath(os.getcwd())}/website/xxy"
    # M3U8.download_video(
    #     m3u8_url="https://video2.tangrenfuli.com/video/20210921/c2d8017defe31bfb7e411eaf4c6a951d/index.m3u8",
    #     save_dir=save_dir,
    #     filename="bbb"
    # )

    save_path = f"{os.path.abspath(os.getcwd())}/website/xxy/res.html"
    with open(save_path, "r", encoding='utf-8') as f:
        html_text = f.read()

        result = re.search(r'\"url\":\"(.+?)\"', html_text)
        play_href = None

        if result:
            play_href = result.group(2)
            print(play_href)


